// v2010.09.21

/*
Copyright (c) 2010 Ben Hendel-Doying

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package game;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.ArrayList;


import world.Actor;
import world.Angel;
import world.Appearance;
import world.Buff;
import world.Camera;
import world.Gate;
import world.Shop;
import world.World;
import game.ASCII;

import gamesource.GameState;

public final class GameStatePlaying implements GameState
{
	private GamePanel controller;
	private World world;
	private Actor player;
	private Camera camera;
	private ArrayList<String> message_history = new ArrayList<String>();
	private ArrayList<String> current_messages = new ArrayList<String>();
	private boolean obol_revealed = false;

	public GameStatePlaying(GamePanel c, World w, Actor p)
	{
		controller = c;

		world = w;
		player = p;

		camera = new Camera(-20, -13, 40, 29);
		camera.CenterOn(player.GetX(), player.GetY());

		player.Tell("Press 'h' at any time for help.");
	}

	public void Animate()
	{
		while(player.HasMessages())
		{
			String m = player.GetMessage();

			current_messages.add(m);
			message_history.add(m);
		}
	}

	public void Render(Graphics g)
	{
		if(player.IsDead())
			return;

		ASCII.DrawString(g, player.GetName(), ASCII.COLOR_WHITE, 0, 0);
		ASCII.DrawString(g, Character.toString((char)3), ASCII.COLOR_RED, 12, 0);
		ASCII.DrawString(g, player.GetHP() + "/" + player.GetMaxHP(), ASCII.COLOR_WHITE, 13, 0);

		if(obol_revealed || player.GetObol() > 0)
		{
			obol_revealed = true;

			String obol = String.valueOf(player.GetObol());

			ASCII.DrawString(g, obol, ASCII.COLOR_WHITE, 25 - obol.length(), 0);
			ASCII.DrawString(g, Character.toString((char)9), ASCII.COLOR_LIGHT_GRAY, 25, 0);
		}

		final ArrayList<Buff> buffs = player.GetBuffs();
		if(buffs.size() > 0)
		{
			for(int i = 0; i < buffs.size(); ++i)
			{
				final Appearance a = buffs.get(i).GetAppearance();
				ASCII.DrawCharacter(g, a.GetCharacter(), a.GetColor(), 30 + i, 0);
			}
		}
		/*
		ASCII.DrawString(g, "XP: " + player.GetExperience(), ASCII.COLOR_WHITE, 30, 0);
		ASCII.DrawString(g, "Demonism: " + player.GetTile().GetMaxDemonism(), ASCII.COLOR_WHITE, 0, 29);
		ASCII.DrawString(g, "Wall Dist: " + player.GetTile().GetMinimumDistanceFromWall(), ASCII.COLOR_WHITE, 20, 29);
		 */

		world.Render(g, camera, 0, 1);

		for(int i = 0; i < current_messages.size(); ++i)
			ASCII.DrawString(g, current_messages.get(i), ASCII.COLOR_LIGHT_GRAY, 0, 1 + i);
	}

	private void ClearCurrentMessages()
	{
		current_messages = new ArrayList<String>();
	}

	public void KeyType(KeyEvent e)
	{
	}

	public void KeyPress(KeyEvent e)
	{
		switch(e.getKeyCode())
		{
			case KeyEvent.VK_ESCAPE:
				controller.ConfirmQuit();
				break;

			case KeyEvent.VK_SLASH:
			case KeyEvent.VK_H:
				controller.Help();
				break;

			case KeyEvent.VK_BACK_QUOTE:
				controller.Credits();
				break;
		}

		if(player.IsDead())
			return;

		boolean step = false;

		switch(e.getKeyCode())
		{
			case KeyEvent.VK_8: case KeyEvent.VK_UP: step = player.MoveNorth(); break;
			case KeyEvent.VK_9: case KeyEvent.VK_PAGE_UP: step = player.MoveNE(); break;
			case KeyEvent.VK_6: case KeyEvent.VK_RIGHT: step = player.MoveEast(); break;
			case KeyEvent.VK_3: case KeyEvent.VK_PAGE_DOWN: step = player.MoveSE(); break;
			case KeyEvent.VK_2: case KeyEvent.VK_DOWN: step = player.MoveSouth(); break;
			case KeyEvent.VK_1: case KeyEvent.VK_END: step = player.MoveSW(); break;
			case KeyEvent.VK_4: case KeyEvent.VK_LEFT: step = player.MoveWest(); break;
			case KeyEvent.VK_7: case KeyEvent.VK_HOME: step = player.MoveNW(); break;
			case KeyEvent.VK_I: controller.Inventory(); break;
			case KeyEvent.VK_Z: controller.CharacterStats(); break;
			case KeyEvent.VK_S: step = player.Search(); break;
			case KeyEvent.VK_COMMA: step = player.PickUp(); break;
			case KeyEvent.VK_C: step = player.CloseDoor(); break;

			case KeyEvent.VK_P:
				if(((Angel)player).Pray())
				{
					player.Tell("You pray...");
					player.Rest();
					controller.Pray();
				}
				else
					player.Tell("The Demonism here is too strong!");

				break;

			case KeyEvent.VK_DELETE:
			case KeyEvent.VK_PERIOD:
			case KeyEvent.VK_SPACE:
				step = player.Rest();
				break;
		}

		if(player.RequestingInput() != Actor.REQUEST_INPUT_NONE)
		{
			switch(player.RequestingInput())
			{
				case Actor.REQUEST_INPUT_GATE:
					controller.Gate((Gate)player.RequestingObject());
					break;
				case Actor.REQUEST_INPUT_SHOP:
					controller.Shop((Shop)player.RequestingObject());
					break;
			}

			player.ClearRequest();
		}

		if(step)
			Step();
	}

	public void Step()
	{
		player.Explore();
		ClearCurrentMessages();
		world.Step();

		if(player.IsDead())
			controller.GameOver();
		else
			camera.CenterOn(player.GetX(), player.GetY());
	}

	public void KeyRelease(KeyEvent e)
	{
	}

	public void MouseClick(MouseEvent e)
	{
	}

	public void MouseDrag(MouseEvent e)
	{
	}

	public void MouseMove(MouseEvent e)
	{
	}

	public void MousePress(MouseEvent e)
	{
	}

	public void MouseRelease(MouseEvent e)
	{
	}

	public void MouseWheeled(MouseWheelEvent e)
	{
	}
}
